package com.kbbywyclient.widget.sml;

import android.content.Context;
import android.content.res.TypedArray;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.kbbywyclient.R;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshKernel;
import com.scwang.smartrefresh.layout.constant.RefreshState;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;

/**
 * Created by Jiangzx on 2017/9/7 10:15.
 */

public class HeaderRefresh extends LinearLayout implements RefreshHeader {
    private static final String PULL_TO_REFRESH = "下拉刷新";
    private static final String RELEASE_TO_REFRESH = "松开开始刷新";
    private static final String REFRESH_ING = "正在刷新...";
    private static final String REFRESH_COMPLETE = "刷新完成";
    private float mTextSize=14;//默认字体大小 14sp
    private int mTextColor=0xFF4BD2CC;//默认颜色
    TextView textView;
    public HeaderRefresh(Context context) {
        this(context,null);
    }

    public HeaderRefresh(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public HeaderRefresh(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        textView=new TextView(context);
        textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
                LayoutParams.WRAP_CONTENT));
        textView.setTextColor(mTextColor);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,mTextSize);
        textView.setText(PULL_TO_REFRESH);
        if(attrs!=null){
            TypedArray array=context.obtainStyledAttributes(attrs, R.styleable.HeaderRefresh);
            int count=array.length();
            int attr;
            for(int i=0;i<count;i++){
                attr=array.getIndex(i);
                switch (attr){
                    case R.styleable.HeaderRefresh_headerTextColor:
                        //文字颜色，默认为黑色
                        textView.setTextColor(array.getColor(attr,mTextColor));
                        break;
                    case R.styleable.HeaderRefresh_headerTextSize:
                        //文字大小，默认15sp
                        int fontSize=array.getDimensionPixelSize(attr,0);
                        if(fontSize>0){
                            textView.setTextSize(TypedValue.COMPLEX_UNIT_PX,fontSize);
                        }else{
                            textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,mTextSize);
                        }
                        break;
                }
            }
            array.recycle();
        }
        addView(textView);
        setGravity(Gravity.CENTER);
    }

    @Override
    public void onPullingDown(float v, int i, int i1, int i2) {

    }

    @Override
    public void onReleasing(float v, int i, int i1, int i2) {

    }

    @NonNull
    @Override
    public View getView() {
        return this;
    }

    @Override
    public SpinnerStyle getSpinnerStyle() {
        return SpinnerStyle.Translate;
    }

    @Override
    public void setPrimaryColors(int... ints) {

    }

    @Override
    public void onInitialized(RefreshKernel refreshKernel, int i, int i1) {

    }

    @Override
    public void onHorizontalDrag(float v, int i, int i1) {

    }

    @Override
    public void onStartAnimator(com.scwang.smartrefresh.layout.api.RefreshLayout refreshLayout, int i, int i1) {

    }

    @Override
    public int onFinish(com.scwang.smartrefresh.layout.api.RefreshLayout refreshLayout, boolean b) {
        textView.setText(REFRESH_COMPLETE);
        return 0;
    }

    @Override
    public boolean isSupportHorizontalDrag() {
        return false;
    }

    @Override
    public void onStateChanged(com.scwang.smartrefresh.layout.api.RefreshLayout refreshLayout, RefreshState oldState, RefreshState newState) {
        switch (newState){
            case None:
            case PullDownToRefresh:
                textView.setText(PULL_TO_REFRESH);
                break;
            case ReleaseToRefresh:
                textView.setText(RELEASE_TO_REFRESH);
                break;
            case Refreshing:
                textView.setText(REFRESH_ING);
                break;
            case RefreshFinish:
                textView.setText(REFRESH_COMPLETE);
                break;
        }
    }

    public void setTextColor(@ColorInt int textColor){
        this.mTextColor=textColor;
        this.textView.setTextColor(mTextColor);
    }

    public void setTextSize(float textSize){
        this.mTextSize=textSize;
        this.textView.setTextSize(TypedValue.COMPLEX_UNIT_SP,mTextSize);
    }
}
